"""获取文件夹中各文件名并输出"""
import os
path = input("输入要分类的文件地址")
datanames = os.listdir(path)
for i in datanames:
    print(i)
"""编写排序函数，现根据后缀名然后根据文件名，排序文件名"""
def cmp_fun(x, y):
    if x.split('.')[-1] > y.split('.')[-1]:
        return 1
    elif x.split('.')[-1] < y.split('.')[-1]:
        return -1
    else :
        if x.split('.')[0] > y.split('.')[0]:
            return 1
        elif x.split('.')[0] < y.split('.')[0]:
            return -1
        else:
            return 0
        
def extsort(filenames):
    """Sorts given list of filenames based on extension.
        >>> extsort(['a.c', 'a.py', 'b.py', 'bar.txt', 'foo.txt', 'x.c'])
        ['a.c', 'x.c', 'a.py', 'b.py', 'bar.txt', 'foo.txt']
    """
    # your code here
    return sorted(filenames, cmp=cmp_fun)
"""利用冒泡法对文件排序"""
i=0
j=1
while i<len(datanames):
    while j<len(datanames):
        if cmp_fun(datanames[i],datanames[j])==1:
            temp=datanames[i]
            datanames[i]=datanames[j]
            datanames[j]=temp
        j+=1
    i+=1
    j=i+1
"""循环输出文件名和文件大小"""
i=0
while i<len(datanames):
    print(datanames[i])
    import os
    size=os.path.getsize(datanames[i])
    print(size,"\n")
    i+=1
